package com.tencent.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tencent.entity.User;
import com.tencent.mapper.LoginMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * @BelongsProject: health2.2
 * @BelongsPackage: com.tencent.service.impl
 * @ClassName: LoginDetailsService
 * @Description: TODO
 * @Version: 1.0.0
 * @CreateTime: 2022-08-30  15:57
 * @Author: Yangjx
 */
@Service
public class LoginDetailsServiceImpl implements UserDetailsService {

    private LoginMapper loginMapper;
    @Autowired
    public LoginDetailsServiceImpl(LoginMapper loginMapper){
        this.loginMapper = loginMapper;
    }

    @Override
    public UserDetails loadUserByUsername(String name) throws UsernameNotFoundException {

        QueryWrapper qw = new QueryWrapper<>();
        qw.eq("username",name);
        User user = loginMapper.selectOne(qw);
        if (user == null) {
            throw new UsernameNotFoundException("输入的用户名不存在!");
        }

        return org.springframework.security.core.userdetails.User//框架下的User
                .withUsername(user.getUsername())
                .password(user.getPassword())//密文
                .authorities("ROLE_P1")//权限
                .build();
    }
}
